/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.business;

import TimeRecording.tables.Control;
import TimeRecording.tables.Employee;
import TimeRecording.tables.WeeklyTime;
import TimeRecording.tables.WorkingPattern;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author david
 */
public class TimesheetMaintenance {

    private Employee employee;
    private DateRange activeWindow;

    private Collection<WorkingPattern> workingPatterns;
    private Collection<WeeklyTime> weeklyTimes;

    public TimesheetMaintenance(EntityManager entityManager, Employee employee) {

        this.employee = employee;
        activeWindow = Control.getActiveWindow(entityManager);

        workingPatterns = WorkingPattern.
                getWorkingPatternsForEmployeeAndDateRange(
                        entityManager, employee, activeWindow);

        weeklyTimes = WeeklyTime.
                getWeeklyTimeByEmployeeAndDateRange(
                        entityManager, employee, activeWindow);
    }

    public void performHousekeeping(EntityManager entityManager) {

        GregorianCalendar start = new GregorianCalendar();
        start.setTime(activeWindow.getFromDate());

        GregorianCalendar stop = new GregorianCalendar();
        stop.setTime(activeWindow.getToDate());

        while (!start.after(stop)) {

            // Do we have this week in our collection?
            WeeklyTime matchedWeeklyTime = null;
            for (WeeklyTime weeklyTime : weeklyTimes) {
                if (weeklyTime.inRange(start)) {
                    matchedWeeklyTime = weeklyTime;
                    break;
                }
            }

            GregorianCalendar weekday = (GregorianCalendar)start.clone();

            // Should we have this week in our collection?  If there
            // are no applicable patterns then the answer is no
            boolean workingPatternFound = false;
            for (int day = 0; day < 7; day++) {
                for (WorkingPattern workingPattern : workingPatterns) {
                    if (workingPattern.isActive(weekday.getTime())) {
                        workingPatternFound = true;
                    }
                }
                weekday.add(GregorianCalendar.DATE, 1);
            }

            if (workingPatternFound) {
                if (matchedWeeklyTime == null) {

                    // Need to create a timesheet as there is a matching pattern
                    WeeklyTime newTimesheet = new WeeklyTime(
                            employee.getEmployeeId(), start.getTime());
                    entityManager.persist(newTimesheet);
                }
            } else {
                if (matchedWeeklyTime != null) {

                    // Need to delete timesheet as there are no patterns
                    entityManager.remove(matchedWeeklyTime);
                }
            }

            start.add(GregorianCalendar.WEEK_OF_YEAR, 1);
        }
    }

    public static void performAllHousekeeping(EntityManager entityManager) {

        List<Employee> employees = Employee.getActiveEmployees(entityManager);

        for (Employee employee : employees) {

            TimesheetMaintenance maintenance =
                    new TimesheetMaintenance(entityManager, employee);

            maintenance.performHousekeeping(entityManager);
        }
    }
}
